package com.xxx.pool;

import java.util.concurrent.*;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/8/2 9:26
 * @Copyright 博为峰
 */
public class ExecutorDemo {
    public static void main(String[] args) {
        //创建带缓冲区的线程池
//        final ExecutorService cachedPool = Executors.newCachedThreadPool();
//        cachedPool.submit(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("abc");
//            }
//        });
//
//        cachedPool.submit(() -> System.out.println("abc"));
//        //固定线程数的线程池
//        final ExecutorService fixedPool = Executors.newFixedThreadPool(10);
//        //定期地执行线程池
//        final ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(10);
//        //单个 worker 线程的
//        final ExecutorService singlePool = Executors.newSingleThreadExecutor();

        /*
             ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue)
             corePoolSize:核心线程数
             maximumPoolSize:最大线程数

             ArrayBlockingQueue：一个由数组支持的有界阻塞队列
             LinkedBlockingQueue:无界队列
             PriorityBlockingQueue:无界队列
         */
        final ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 15, 1000, TimeUnit.SECONDS, new ArrayBlockingQueue(5));
//        executor.execute(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 10; i++) {
//                    System.out.println(Thread.currentThread().getName() + "," + i);
//                }
//            }
//        });

        for (int i = 0; i < 30; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        System.out.println(Thread.currentThread().getName() + "," + i);
                    }
                }
            });
        }

    }
}
